from typing import Any

from fastapi import status
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse

from app.schemas.common import APIResponse


def success_response(data: Any = None, message: str = "success") -> JSONResponse:
    payload = APIResponse(
        status=status.HTTP_200_OK,
        message=message,
        data=data
    ).model_dump(mode="json")
    return JSONResponse(status_code=status.HTTP_200_OK, content=payload)


def error_response(
        status_code: int,
        message: str,
        data: Any = None
) -> JSONResponse:
    """错误响应"""
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content=APIResponse(
            status=status_code,
            message=message,
            data=data
        ).model_dump()
    )


def pageInfo(
        totalNum: int,
        data: Any = None
) -> JSONResponse:
    """响应分页数据"""
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content=APIResponse(
            status=status.HTTP_200_OK,
            message="请求成功",
            data={"totalNum": totalNum,
                  "data": jsonable_encoder(data)}
        ).model_dump()
    )
